using DealerPlatform.Core;
using DealerPlatform.Service.ProductApp;
using DealerPlatform.Service.ProductApp.Dtos;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace DealerPlatform.Web.Controllers;

// [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ProductController(IProductService productService) : BaseController
{
    [HttpGet]
    public async Task<IEnumerable<ProductDto>> GetProductDto(
        string? searchText,
        string? productType,
        string belongTypeName,
        string? productProps,
        string? sort,
        int pageIndex = 1,
        int pageSize = 30,
        OrderType orderTyp = OrderType.Asc)
    {
        Dictionary<string, string> dicProductProps = new();
        var arrProductProps = productProps?.Split("^") ?? [];
        foreach (var item in arrProductProps)
        {
            var key = item.Split("_")[0];
            var value = item.Split("_")[1];
            dicProductProps.Add(key, value);
        }

        return await productService.GetProductDto(
            searchText,
            productType,
            belongTypeName,
            dicProductProps,
            new PageWithSortDto()
            {
                Sort = sort,
                PageIndex = pageIndex,
                PageSize = pageSize,
                OrderType = orderTyp
            });
    }

    [HttpGet("type")]
    public async Task<IEnumerable<ProductTypeDto>> GetProductTypeDtoAsync(string belongTypeName)
    {
        return await productService.GetProductType(belongTypeName);
    }

    [HttpGet("props")]
    public async Task<Dictionary<string, IEnumerable<string>>> GetProductProps(
        string belongTypeName,
        string? typeNo)
    {
        return await productService.GetProductProps(belongTypeName, typeNo);
    }

    [HttpGet("belongType")]
    public async Task<List<BelongTypeDto>> GetBelongTypeAsync()
    {
        return await productService.GetBelongTypeDtoAsync();
    }
}